package com.hailong.zyt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hailong.zyt.beans.Course;
import com.hailong.zyt.beans.CourseAll;
import com.hailong.zyt.beans.Teacher;
import com.hailong.zyt.mapper.*;
import com.hailong.zyt.service.TeacherService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hailong
 * @since 2021-05-17
 */
@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements TeacherService {

    private TeacherMapper teacherMapper;
    private CourseMapper courseMapper;
    private CourseAllMapper courseAllMapper;
    private StudentMapper studentMapper;


    @Override
    public boolean addTeacher(Teacher newTeacher) {
        //表单验证空位

        boolean saved = this.save(newTeacher);
        return saved;
    }

    @Override
    public Integer getCourseCount(Long teaId) {
        QueryWrapper<Course> wrapper = new QueryWrapper();
        wrapper.eq("tea_id", teaId);
        Integer count = courseMapper.selectCount(wrapper);
        return count == null ? 0 : count;
    }

    @Override
    public Integer getStudentCount(Long teaId) {
        QueryWrapper<CourseAll> wrapper = new QueryWrapper<>();
        wrapper.eq("tea_id", teaId);
        Integer count = courseAllMapper.selectCount(wrapper);
        return count == null ? 0 : count;
    }

    @Override
    public String getHWFinishRate(Long teaId) {
/*        QueryWrapper<Homework> wrapper = new QueryWrapper<>();
        wrapper.eq("tea_id", teaId);
        List<Homework> list = homeworkMapper.selectList(wrapper);
        int count = 0;
        for(Homework homework : list) {
            if(homework.getHwState() != 0) count++;
        }
        String res = "" + count + " / " + list.size();*/
        return null;
    }

    @Override
    public Teacher login(Teacher user) {
        Teacher teacher = teacherMapper.selectById(user.getTeaId());
        if(teacher != null && teacher.getTeaPassword().equals(user.getTeaPassword())) {
            return teacher;
        }
        else {
            return null;
        }
    }

    @Autowired
    public void setTeacherMapper(TeacherMapper teacherMapper) {
        this.teacherMapper = teacherMapper;
    }

    @Autowired
    public void setCourseMapper(CourseMapper courseMapper) {
        this.courseMapper = courseMapper;
    }

    @Autowired
    public void setCourseAllMapper(CourseAllMapper courseAllMapper) {
        this.courseAllMapper = courseAllMapper;
    }


    @Autowired
    public void setStudentMapper(StudentMapper studentMapper) {
        this.studentMapper = studentMapper;
    }
}
